package com.lzb.newloan.utils;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.lzb.newloan.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

public class BaseApplication extends Application {
    private static Context mContext;
    private boolean isDownload;
    private static BaseApplication mApplication;
//    public BaseEventUtils events = new BaseEventUtils();

    public synchronized static BaseApplication getInstance() {
        return mApplication;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        // ShareSDK.initSDK(this);

        Log.e("eeee", "进来的次数");
        // 是否开启debug模式
        mContext = this;
        isDownload = false;
        mApplication = this;

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                // 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_launcher)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_launcher)
                // 设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)
                // 设置下载的图片是否缓存在SD卡中
                .cacheInMemory(true)
                .displayer(new FadeInBitmapDisplayer(100))
                // 是否图片加载好后渐入的动画时间
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .cacheOnDisk(true).displayer(new FadeInBitmapDisplayer(100))
                .bitmapConfig(Bitmap.Config.RGB_565).build();

        File cacheDir = StorageUtils.getOwnCacheDirectory(this,
                "/sdcard/CHAOGS/imageloader/Cache");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCache((new UnlimitedDiscCache(cacheDir))).build();
        ImageLoader.getInstance().init(config);
        super.onCreate();
        // TelephonyManager tm = (TelephonyManager) this
        // .getSystemService(Context.TELEPHONY_SERVICE);
        // String deviceId = tm.getDeviceId();

    }

    public static Context getAppContext() {
        return mContext;
    }

    public boolean isDownload() {
        return isDownload;
    }

    public void setDownload(boolean isDownload) {
        this.isDownload = isDownload;
    }

}
